﻿using System.Windows.Forms;
using OhioState.Graphics;
using OhioState.Math;

namespace OSU_Widgets
{
    public partial class CameraEditorControl : UserControl
    {
        public CameraEditorControl()
            : this(new Camera())
        {
        }
        public CameraEditorControl(ICamera camera)
        {
            InitializeComponent();

            SetCamera(camera);
            //
            // TODO: decide whether the user should change this, and if so, how the
            //    aspect ratio and other settings should change.
            //
            this.viewAngleUpDown.Enabled = false;
        }

        private void SetCamera(ICamera camera)
        {
            currentCamera = camera;
            Vector4 position; Vector4 lookAtDirection; Vector4 viewUp; float viewAngle;
            CameraHelper.ExtractCoordinateFrame(currentCamera, out position, out lookAtDirection, out viewUp, out viewAngle);
            this.positionControl.Value = new Vector3(position.x, position.y, position.z);
            this.lookAtControl.Value = new Vector3(lookAtDirection.x, lookAtDirection.y, lookAtDirection.z);
            this.viewUpControl.Value = new Vector3(viewUp.x, viewUp.y, viewUp.z);
            this.viewAngleUpDown.Value = (decimal)viewAngle;
        }

        public ICamera Camera
        {
            get { return currentCamera; }
            set { SetCamera(value); }
        }

        private ICamera currentCamera;

        private void cameraParamsChanged(object sender, System.EventArgs e)
        {
            Matrix4 modelView = CameraHelper.ComputeModelViewMatrix(positionControl.Value as Vector3, 
                lookAtControl.Value as Vector3, 
                viewUpControl.Value as Vector3);
            currentCamera.SetModelViewMatrix(modelView);
        }

        private void refreshButton_Click(object sender, System.EventArgs e)
        {
            SetCamera(currentCamera);
        }
    }
}
